package br.com.sigde.framework.apresentacao.componente.mensagens;

import java.io.IOException;
import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.apache.myfaces.renderkit.html.HtmlMessagesRenderer;
import org.apache.myfaces.shared_impl.renderkit.html.HTML;
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;
import org.apache.myfaces.shared_impl.util.NullIterator;


public class HtmlMensagemRendererFW extends HtmlMessagesRenderer {


	public static final String RENDERER_MAPPING = "htmlMensagemRendererFW";

	@Override
	protected void renderMessages(FacesContext facesContext,UIComponent messages)
	throws IOException {
		MessagesIterator messagesIterator = new MessagesIterator(facesContext,isGlobalOnly(messages));
		if (messagesIterator.hasNext()) {
			renderList(facesContext, messages, messagesIterator);
		}
	}

	private void renderList(FacesContext facesContext, UIComponent messages, MessagesIterator messagesIterator)
	throws IOException
	{
		ResponseWriter writer = facesContext.getResponseWriter();

		//writer.startElement(HTML.UL_ELEM, messages);
		writer.startElement(HTML.SCRIPT_ELEM, messages);
		
		HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext);
		
		writer.write("alert(html_entity_decode('");
		
		while(messagesIterator.hasNext()) {
			FacesMessage message = messagesIterator.next();
			writer.write(message.getSummary());
			writer.write("\\n");
		}
		writer.write("'));");
		writer.endElement(HTML.SCRIPT_ELEM);
	}

	protected void renderSingleFacesMessage(FacesContext facesContext,
											UIComponent message,
											FacesMessage facesMessage,
											String messageClientId) throws IOException {

		//Adicionar cor a severidade da mensagem.
		facesMessage.getSeverity();
		
		String summary = getSummary(facesContext, message, facesMessage, messageClientId);
		String detail = getDetail(facesContext, message, facesMessage, messageClientId);
		ResponseWriter writer = facesContext.getResponseWriter();
		
		boolean showSummary = isShowSummary(message) && (summary != null);
		boolean showDetail = isShowDetail(message) && (detail != null);

		if(getFor(message) != null){
			writer.writeText(getFor(message), null);
		}
		
		if (showSummary) {
			writer.writeText(summary, null);
		}
		if (showDetail) {
			writer.writeText(detail, null);
		}
	}

	@SuppressWarnings("unchecked")
	private static class MessagesIterator implements Iterator<FacesMessage> {

		private FacesContext _facesContext;
		private Iterator<FacesMessage> _globalMessagesIterator;
		private Iterator<String> _clientIdsWithMessagesIterator;
		private Iterator<FacesMessage> _componentMessagesIterator = null;
		private String _clientId = null;

		public MessagesIterator(FacesContext facesContext, boolean globalOnly)
		{
			_facesContext = facesContext;
			if (globalOnly)
			{
				_globalMessagesIterator = facesContext.getMessages(null);
				_clientIdsWithMessagesIterator = NullIterator.instance();
			}
			else
			{
				_globalMessagesIterator = org.apache.myfaces.shared_impl.util.NullIterator.instance();
				_clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages();
			}
			_componentMessagesIterator = null;
			_clientId = null;
		}

		public boolean hasNext()
		{
			return _globalMessagesIterator.hasNext() ||
			_clientIdsWithMessagesIterator.hasNext() ||
			(_componentMessagesIterator != null && _componentMessagesIterator.hasNext());
		}

		public FacesMessage next()
		{
			if (_globalMessagesIterator.hasNext())
			{
				return _globalMessagesIterator.next();
			}
			else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext())
			{
				return _componentMessagesIterator.next();
			}
			else
			{
				_clientId = (String)_clientIdsWithMessagesIterator.next();
				_componentMessagesIterator = _facesContext.getMessages(_clientId);
				return _componentMessagesIterator.next();
			}
		}

		public void remove()
		{
			throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
		}

		public String getClientId()
		{
			return _clientId;
		}
	}

}
